package Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.util.Log;

import com.example.idfsgm.util.NotEnoughParticipantsException;

import datatypes.Assignment;
import datatypes.Duty;
import datatypes.GuardDuty;
import datatypes.Service;
import datatypes.Soldier;

public class ServiceManager {

	/* ------------- manager functions ------------- */

	public static Assignment assignDuties(List<Soldier> potentialAppointees,
			Service service) throws NotEnoughParticipantsException {

		Assignment ass = new Assignment();

		if (potentialAppointees.size() < service.numOfParticipants) {
			throw new NotEnoughParticipantsException();
		}

		int appointeesCounter = 0;
		Soldier minPointSoldier = null;
		Duty assignedDuty = null;

		Collections.sort(potentialAppointees);
		service.sortDuties();

		while (appointeesCounter < service.numOfParticipants) {

			// get minimal soldier and maximal duty
			minPointSoldier = Collections.min(potentialAppointees);
			assignedDuty = service.getMostExpensiveDuty();

			// assign duty
			minPointSoldier.addDuty(assignedDuty);

			Log.i("LOG_TAG", "assigned duty " + assignedDuty.getDID()
					+ " for soldier " + minPointSoldier.getId());

			ass.addDutyAssignment(minPointSoldier, assignedDuty);

			// remove soldier and duty from arrangements
			potentialAppointees.remove(minPointSoldier);
			service.removeDuty(assignedDuty.getDID());
			appointeesCounter++;
		}

		return ass;

	}

}
